package com.slt.concurrency.mashibing2019.juc.c_007_LockAndTools;

import java.util.concurrent.locks.ReentrantLock;

/**
 * new ReentrantLock(true) 公平锁
 * 默认为非公平锁
 * <p>
 * t1执行完以后，可能会比t2更快进入等待队列，这样下次仍然有可能是t1执行。
 */
public class T05_ReentrantLock5 extends Thread {

    private static ReentrantLock lock = new ReentrantLock(true);

    public void run() {
        for (int i = 0; i < 100; i++) {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "线程");
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        T05_ReentrantLock5 rl = new T05_ReentrantLock5();
        Thread th1 = new Thread(rl);
        Thread th2 = new Thread(rl);
        th1.start();
        th2.start();
    }
}
